import {
  selectCourseTeachList
} from '@/api/teach.js'
import {
  getClassByGrade
} from '@/api/basic.js'
import {
  selectStuAttendlist
} from '@/api/attend.js'
import CourseChapter from '@/components/Course/CourseChapter/index.vue'
export default {
  components: {
    CourseChapter
  },
  data() {
    return {
      teacherDatas: [],
      classDatas: [],
      studentParam: {
        courseCatalogId: this.$route.query.courseCatalogId || null, // 课程章节
        stuAttendUserId: '', // 老师ID
        stuAttendClass: '' // 班级名称
      },
      studentDatas: [],
      statistics: {
        signin: 0, // 签到总数
        absence: 0 // 未到总数
      }
    }
  },
  created() {
    this.getDatas()
  },
  watch: {
    studentParam: {
      handler() {
        this.selectStuAttendlist()
      },
      deep: true
    },
    $route: {
      handler() {
        this.studentParam.courseCatalogId = this.$route.query.courseCatalogId
        this.selectStuAttendlist()
      },
      deep: true
    }
  },
  methods: {
    getDatas() {
      this.selectCourseTeachList()
      this.getClassByGrade()
      if (this.studentParam.courseCatalogId) {
        this.selectStuAttendlist()
      }
    },
    selectCourseTeachList() { // 获取授课老师列表
      selectCourseTeachList({
        courseId: this.$route.query.courseId
      }).then(response => {
        if (response.code === 20000 || response.code === 200) {
          // 对老师进行去重
          var obj = {};
          this.teacherDatas = response.data.reduce(function(item, next) {
            obj[next.courseTeachUserId] ? '' : obj[next.courseTeachUserId] = true && item.push(next);
            return item;
          }, [])
        } else {
          this.$message({
            type: 'info',
            message: '班级获取失败'
          })
        }
      })
    },
    getClassByGrade() { // 获取班级列表
      getClassByGrade().then(response => {
        if (response.code === 20000 || response.code === 200) {
          this.classDatas = response.data
        } else {
          this.$message({
            type: 'info',
            message: '班级获取失败'
          })
        }
      })
    },
    selectStuAttendlist() { // 获取考勤学生列表
      selectStuAttendlist(this.studentParam).then(response => {
        if (response.code === 20000 || response.code === 200) {
          // console.log(response.data)
          this.studentDatas = response.data
          this.statistics.signin = response.total
          this.statistics.absence = response.data.length - response.total
          // console.log(this.studentDatas)
        } else {
          this.$message({
            type: 'info',
            message: '学生获取失败'
          })
        }
      })
    },
    formParamSubmit() {

    }
  }
}
